package com.fxn.adapters;

import com.fxn.pix.ResourceTable;
import com.fxn.modals.Img;
import com.fxn.interfaces.OnSelectionListener;
import com.fxn.utils.PixelUtil;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Image;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * description : MainAbility
 *
 * @since 2021-04-15
 */
public class InstantImageAdapter extends BaseItemProvider {
    private static final int MARGIN = (int) PixelUtil.vp2px(3);

    private ArrayList<Img> mList = new ArrayList<>();
    private Context mContext;
    private OnSelectionListener onSelectionListener;
    private StackLayout.LayoutConfig mLayoutConfig;

    /**
     * 初始化 HorizontalProvider
     *
     * @param context context
     */
    public InstantImageAdapter(Context context) {
        mContext = context;
        int size = (int) PixelUtil.vp2px(82);
        mLayoutConfig = new StackLayout.LayoutConfig(size, size);
    }

    /**
     * 添加 onSelectionListener监听
     *
     * @param onSelectionListener 监听回调
     */
    public void addOnSelectionListener(OnSelectionListener onSelectionListener) {
        this.onSelectionListener = onSelectionListener;
    }

    /**
     * 设置HorizontalProvider 的数据源list
     *
     * @param list list
     */
    public void setList(ArrayList<Img> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemComponentType(int position) {
        Img image = mList.get(position);
        return (image.getContentUrl().isEmpty()) ? MainImageAdapter.HEADER : MainImageAdapter.ITEM;
    }

    /**
     * 设置item是否选中
     *
     * @param selection item是否选中 true为选中，false为取消选中
     * @param pos       item所在的position
     */
    public void select(boolean selection, int pos) {
        if (pos < 100) {
            mList.get(pos).setSelected(selection);
            notifyDataSetItemChanged(pos);
        }
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Img image = mList.get(position);
        ViewHolder viewHolder = null;
        Component tempComponent = null;
        if (component == null) {
            viewHolder = new ViewHolder();
            tempComponent = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_main_image, null, false);
            if (tempComponent.findComponentById(ResourceTable.Id_selection) instanceof Image) {
                viewHolder.check = (Image) tempComponent.findComponentById(ResourceTable.Id_selection);
            }
            if (tempComponent.findComponentById(ResourceTable.Id_preview) instanceof Image) {
                viewHolder.preview = (Image) tempComponent.findComponentById(ResourceTable.Id_preview);
            }
            if (tempComponent.findComponentById(ResourceTable.Id_isVideo) instanceof Image) {
                viewHolder.videoIcon = (Image) tempComponent.findComponentById(ResourceTable.Id_isVideo);
            }
            if (position == mList.size() - 1) {
                mLayoutConfig.setMargins(0, 0, 0, 0);
            } else {
                mLayoutConfig.setMarginRight(MARGIN);
            }
            tempComponent.setLayoutConfig(mLayoutConfig);
            tempComponent.setTag(viewHolder);
        } else {
            tempComponent = component;
            if (tempComponent.getTag() instanceof ViewHolder) {
                viewHolder = (ViewHolder) tempComponent.getTag();
            }
        }
        // 根据itemType设置布局
        setItemData(position, image, viewHolder, tempComponent);
        return tempComponent;
    }

    private void setItemData(int position, Img image, ViewHolder viewHolder, Component tempComponent) {
        if (getItemComponentType(position) == MainImageAdapter.HEADER) {
            StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig(0, 0);
            tempComponent.setLayoutConfig(layoutConfig);
            tempComponent.setVisibility(Component.HIDE);
        } else {
            if (viewHolder != null) {
                if (image.getMediaType().contains("image")) {
                    // 图片
                    viewHolder.videoIcon.setVisibility(Component.HIDE);
                } else {
                    // 视频
                    viewHolder.videoIcon.setVisibility(Component.VISIBLE);
                }
                viewHolder.check.setVisibility(image.getSelected() ? Component.VISIBLE : Component.HIDE);
                viewHolder.preview.setPixelMap(image.getPixelMap());
                ComponentContainer.LayoutConfig layoutConfig = tempComponent.getLayoutConfig();
                if (position < mList.size() - 1) {
                    layoutConfig.setMarginRight(10);
                }
                tempComponent.setLayoutConfig(layoutConfig);
                tempComponent.setVisibility(Component.VISIBLE);
                viewHolder.preview.setClickedListener(component1 -> {
                    if (onSelectionListener != null) {
                        onSelectionListener.onClick(image, component1, position);
                    }
                });
                viewHolder.preview.setLongClickedListener(component1 -> {
                    if (onSelectionListener != null) {
                        onSelectionListener.onLongClick(image, component1, position);
                    }
                });
            }
        }
    }

    private static class ViewHolder {
        Image preview;
        Image check;
        Image videoIcon;
    }
}
